package Tast;

import java.util.HashMap;
import java.util.Map;

public class Test2 {


        public static void main1(String[] args) {
            int year=1;
            int res=0;
            while(year<=2020){
                if(check(year)) res++;
                year++;
            }
            System.out.println(res);
        }

        private static boolean check(int year) {
            while(year>0){
                if(year%10==2) return true;
                year = year/10;
            }
            return false;
        }


        public int findMaxLength(int[] nums) {
            Map<Integer , Integer> hash = new HashMap<Integer , Integer>();
            hash.put(0 , -1);//默认存在一个前缀和为0的情况
            int sum = 0, ret = 0;
            for(int i = 0; i < nums.length;i++){
                sum += (nums[i] == 0 ? -1:1);
                if(hash.containsKey(sum)) ret = Math.max(ret , i - hash.get(sum));
                else hash.put(sum , i);
            }
            return ret;
        }

    public static void main(String[] args) {
        int[] arrya ={1,0,1,0,0,1};
        Test2 test2 = new Test2();
        test2.findMaxLength(arrya);
        System.out.println(test2);
    }

    }


